package com.example.demo.Schedule;

import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @ClassName PrintTask
 * @Description TODO
 * @Author hly
 * @Date 2018\11\29 0029 18:15
 */
@Slf4j
@Component
public class PrintTask {

    private boolean flag = false;

    public static String result1 = null;

    @Scheduled(cron = "0/5 49 10 * * ? ")
    public void corn(){
        System.out.println("执行corn的date是："+new Date(System.currentTimeMillis()));
    }
    File file = new File("/checkTask.txt");
    FileWriter fw = null;


    public PrintTask(boolean flag) {
        this.flag = flag;
    }

    @Scheduled(cron = "0 28 14 * * ?")
    public void test()  {
        try {
            //创建一个线程
            Thread thread = new Thread(new Exetask());
            //开始执行任务
            thread.start();
            log.info("识别任务开始,当前时间 = {}",new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS").format(new Date()));

            fw.write("识别任务开始,当前时间 :"+new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS").format(new Date()));

        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    @Scheduled(cron = "10 29 14 * * ?")
    public void over() {
        flag = true;
        log.info("停止线程");
    }

    private class Exetask implements Runnable {
        @Override
        public void run() {
            File file = new File("C:\\equipment\\");
            File[] listFiles = file.listFiles();
            List<File> files = new ArrayList<File>();
            for(File f:listFiles){
                if (f.getName().substring(0,1).equals("A")){
                    files.add(f);
                }
            }
            if (files.size()>0){
                log.info("开始发送照片，开始任务时间 = {},目前执行任务的线程是 = {}", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS").format(new Date()),Thread.currentThread().getName());
                //执行的任务
                for (int i = 0; i < files.size(); i++) {
                    long beginTime = System.currentTimeMillis();
                    try {
                        if (flag) {
                            throw new InterruptedException();
                        }
                        //处理逻辑
                        String base64 = FileToBase64.encodeBase64File(files.get(i).getPath());
                        String sendParam = UrlUtil.getURLEncoderString(base64);
                        //发送 POST 请求
                        String url = "http://10.0.0.195:5011/preset/";
                        String param = "class_id="+6+"&photo="+sendParam+"&total_num=1"+"&camera_id=1"+"&NO=1";
                        log.info("发送的照片名是 = {},当前时间是 = {}",files.get(i).getName(),new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS").format(new Date()));
                        fw.write("发送的照片名是:"+files.get(i).getName()+" 当前时间是:"+new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS").format(new Date()));
                        String result=HttpRequest.sendPost(url,param);
//                        result1 = result;
                        log.info("识别拿到的返回结果是 = {} ,当前时间 = {}",result,new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS").format(new Date()));
                        fw.write("发送的照片名是:"+files.get(i).getName()+" 当前时间是:"+new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS").format(new Date()));
                        long endTime = System.currentTimeMillis();
                        log.info("程序运行时间 = {}",(endTime-beginTime)/1000);
                        log.info("\n");
                        fw.close();
                    } catch (Exception e) {
                        log.info(Thread.currentThread().getName() + "  stop!!!");
                        try {
                            fw.write(Thread.currentThread().getName() + "  stop!!!");
                        } catch (IOException e1) {
                            e1.printStackTrace();
                        }
                        break;
                    }
                }
            }
            //如果文件数量为0

        }
    }






    public static void main(String[] args) {
        File file = new File("C:\\equipment\\");
        File[] listFiles = file.listFiles();
        List<File> files = new ArrayList<File>();
        for(File f:listFiles){
            if (!f.getName().substring(0,1).equals("A")){
                files.add(f);
            }
        }
        files.stream().map(x->x.getName()).forEach(System.out::println);

    }

}
